package cn.myblog.ainblog.conroller.backstage;

import cn.myblog.ainblog.dto.UserDTO;
import cn.myblog.ainblog.model.UserModel;
import cn.myblog.ainblog.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import java.net.http.HttpResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Dovaior
 * @Description 关于用户(User)的控制器
 * @create 2021-03-21 22:20
 **/
@Controller
@RequestMapping("/backstage")
public class BackstageUserController {
    @Autowired
    private UserService service;

    /**
     * 查询用户
     * @param session 在session中的查询
     * @param model 封装信息给前端(提示信息)
     * @return 返回页面
     */
    @GetMapping("/selectUser")
    public String select(HttpSession session, Model model) {
        String loginUser = (String) session.getAttribute("LoginUser");
        Long userId = service.selectUserIdByUsername(loginUser);
        UserModel userModel = service.selectById(userId);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userModel, userDTO);
        //执行自定义方法，覆盖md5
        customize(session, userDTO);
        model.addAttribute("userDTO", userDTO);
        return "backstage/backstage-personalInformation";
    }

    /**
     * 更新用户信息(回显信息)
     * @param id 获取ID查询用户
     * @param session 修改的信息将session的值覆盖
     * @return 返回封装信息
     */
    @ResponseBody
    @GetMapping("/updateById/{id}")
    public UserDTO updateById(@PathVariable Long id, HttpSession session) {
        UserModel userModel = service.selectById(id);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userModel, userDTO);
        //执行自定义方法，覆盖md5值
        customize(session, userDTO);
        return userDTO;
    }

    /**
     * 更新用户
     * @param session 将session中的值覆盖
     * @param userDTO 接收来自前端的映射
     * @return 返回页面
     */
    @ResponseBody
    @PostMapping("/updateUserInfo")
    public String updateById(HttpSession session, UserDTO userDTO) {
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDTO, userModel);
        //将修改的用户名加密
        String username = DigestUtils.md5DigestAsHex(userModel.getUserName().getBytes());
        //将修改的密码加密
        String password = DigestUtils.md5DigestAsHex(userModel.getPassword().getBytes());
        //赋值
        userModel.setUserName(username);
        userModel.setPassword(password);
        service.updateById(userModel);
        //判断密码或用户名是否修改
        if ((!userDTO.getUserName().equals(session.getAttribute("username")))||(!userDTO.getPassword().equals(session.getAttribute("password")))){
            //清除session
            session.invalidate();
        }
        return "redirect:/backstage/selectUser";
    }

    /**
     * 自定义方法：封装赋值操作
     * @param session 查询session中的值
     * @param userDTO 覆盖userDto中的属性
     */
    public void customize(HttpSession session, UserDTO userDTO) {
        String userName = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        userDTO.setUserName(userName);
        userDTO.setPassword(password);
    }
}
